package cn.tangsihai.controller;

import cn.tangsihai.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getById/{id}")
    @HystrixCommand(fallbackMethod = "backOrderGet")   //方法熔断
    public User orderGet(@PathVariable("id") Long id){
        String url = "http://SERVICE-USER/getById/" + id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    // 拖底方法
    public User backOrderGet(@PathVariable("id") Long id){
        return new User(-1L, "用户服务异常");
    }
}
